#include "uart.h"

int uart_open(int comport)
{
    int fd = 0;
    // char *dev[] = {"/dev/ttyAMA0","/dev/ttyAMA1","/dev/ttyAMA2"};
    char *dev[] = {"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
    printf("开始执行串口函数\n");
    
    switch(comport)
    {
        case 0:
        fd = open(dev[0],O_RDWR|O_NOCTTY|O_NDELAY);
        if ( -1 == fd){
            perror("Fail to Open Serial Port");
            return -1;
        }
        break;
        case 1:
        fd = open(dev[1],O_RDWR|O_NOCTTY|O_NDELAY);
        if ( -1 == fd){
            perror("Fail to Open Serial Port");
            return -1;
        }
        break;
        case 2:
        fd = open(dev[2],O_RDWR|O_NOCTTY|O_NDELAY);
        if ( -1 == fd){
            perror("Fail to Open Serial Port");
            return -1;
        }
        break;
    }
    printf("串口打开成功\n");

    // 设置串口为阻塞状态，用于等待串口数据的读入
    if(fcntl(fd, F_SETFL, 0) < 0 )
        printf("fcntl failed!\n");
    else
        printf("fcntl = %d\n",fcntl(fd, F_SETFL, 0));
    //测试是否为终端设备
    if(isatty(STDIN_FILENO) == 0)
        printf("standard input is not a terminal device \n");
    else
        printf("isatty success!\n");
    printf("串口程序执行完毕fd-open=%d\n",fd);
    return fd;
}